<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use service\DataService;
use service\LogService;
use controller\BasicAdmin;
use think\Db;

/**
 * 友链管理
 * Class Links
 * @package app\admin\controller
 * @author  
 * @date 2017/9/25
 */
class Links extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'LinksItem';

    /**
     * 指定当前广告位数据表
     * @var string
     */
    public $linksTable = 'Links';

    /**
     * Links 列表
     * @return mixed
     */
    public function index()
    {
        $links_id = $this->request->param('links_id');
        $this->assign('links_id', $links_id);
        $this->title = '友链管理 - '. $this->get_links_name_byid($links_id);
        $get = $this->request->get();
        // 实例Query对象
        $map = [];
        if(isset($links_id)){
            $map['links_id'] = $links_id;
        }
        $db = Db::name($this->table)->where($map);

        // 应用搜索条件
        foreach (['title'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }

        // 友链位置名称
        $data = parent::_list($db, true, false);
        foreach($data['list'] as $key=>$list) {
            $data['list'][$key]['links_name'] = $this->get_links_name_byid($list['links_id']);
        }
        return $this->fetch('', $data);
    }

    /**
     * 添加
     */
    public function add() {
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑
     */
    public function edit() {
        return $this->add();
    }

    /**
     * 删除
     */
    public function del() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '链接删除成功');
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用
     */
    public function forbid() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '链接禁用成功');
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 启用
     */
    public function resume() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '链接启用成功');
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

    /**
     * 友链位置 表单数据前缀方法
     * @param array $vo
     */
    protected function _form_filter(&$vo) {
        if ($this->request->isGet()) {
            // 友链位置处理
            $linksList = Db::name($this->linksTable)->order('sort asc, id asc')->select();
            $this->assign('linksList', $linksList);
            $links_id = $this->request->param('links_id');
            if($links_id){
                $vo['links_id'] = $links_id;
            }
            $linksData = Db::name($this->linksTable)->where('id', $links_id)->find();
            $this->assign('linksData', $linksData);
        }
    }

    /**
     * 友链位置列表
     */
    public function links()
    {
        $this->title = '友链位置管理';
        $db = Db::name($this->linksTable)->order('sort asc, id asc');
        return parent::_list($db);
    }

    /**
     * 根据友链位置id获取友链位置名称
     * @param int $links_id
     * @return mixed
     */
    private static function get_links_name_byid($links_id) {
        $result = Db::name('links')->where('id', $links_id)->value('name');
        return $result;
    }

    /**
     * 添加友链位置
     */
    public function addLinks() {
        return $this->_form($this->linksTable, 'linksform');
    }

    /**
     * 编辑友链位置
     */
    public function editLinks() {
        return $this->addLinks();
    }

    /**
     * 删除友链位置
     */
    public function delLinks() {
        if (DataService::update($this->linksTable)) {
            LogService::write('系统设置', '链接位置删除成功');
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用友链位置
     */
    public function forbidLinks() {
        if (DataService::update($this->linksTable)) {
            LogService::write('系统设置', '链接位置禁用成功');
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 启用友链位置
     */
    public function resumeLinks() {
        if (DataService::update($this->linksTable)) {
            LogService::write('系统设置', '链接位置启用成功');
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }




}
